/* eslint-disable no-empty */
/* eslint-disable no-console */
/* eslint-disable handle-callback-err */
/* eslint-disable camelcase */
/* eslint-disable no-undef */
/* eslint-disable space-before-function-paren */
/* eslint-disable indent */
/* eslint-disable global-require */
/* eslint-disable no-negated-condition */
/* eslint-disable no-unused-vars */
var baidu = require('baidu-aip-sdk');
var path = require('path');
var fs = require('fs');
var Recorder = require('js-audio-recorder');
var {
  shell,
  ipcRenderer
} = require('electron');
var SerialPort = require('serialport');
const getProjectTitleFromFilename = fileInputFilename => {
  if (!fileInputFilename) return '';
  const matches = fileInputFilename.match(/^(.*)\.sb[23]?$/);
  if (!matches) return '';
  return matches[1].substring(0, 100); // truncate project title to max 100 chars
};

const readerx = fileToUpload => {
  let reader = new FileReader();
  reader.readAsArrayBuffer(fileToUpload);

  reader.onload = e => {
    vm.loadProject(reader.result)
      .then(_e => {
        try { // Can fail e.g. when GUI is loaded from static file (integration tests)
          history.replaceState({}, document.title, '.');
        } catch (err) { }
      })
      .catch(error => {
      });
  };
};


class Initclass {
  constructor() {
    this.AppID = '16400774';
    this.APIKey = 'CeOuAOvi7QZWPMXdjQWbPuP3';
    this.SecretKey = 'gPLS7F91SB7AGIlQSunqufX70oKRiASm';
    this.Recorder = Recorder;
    this.shell = shell;
    this.ipcRenderer = ipcRenderer;
    this.SerialPort = SerialPort;
  }
  setFaceinit(image, option = {}) {
    const AipFaceClient = baidu.face;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    const image_type = 'BASE64';
    option.face_field = 'age,beauty,expression,faceshape,gender,glasses,race,facetype';
    option.face_type = 'LIVE';
    option.max_face_num = '1';
    return client.detect(image, image_type, option);

  }

  setgesture(image, option = {}) {
    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.gesture(image, option);

  }

  setbodyAttrtopwear(image, option = {}) {
    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option.type = 'lower_wear,upper_wear';
    return client.bodyAttr(image, option);
  }


  setbodyAttrdownwear(image, option = {}) {

    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option.type = 'lower_wear,upper_wear';
    return client.bodyAttr(image, option);
  }


  setbodyNum(image, option = {}) {


    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);

    return client.bodyNum(image, option);

  }


  setadvancedGeneral(image, option = {}) {

    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.advancedGeneral(image, option);

  }


  setdishDetect(image, option = {}) {

    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option.top_num = '1';
    return client.dishDetect(image, option);

  }


  setcarDetect(image, option = {}) {

    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option.top_num = '1';
    return client.carDetect(image, option);

  }


  setlogoSearch(image, option = {}) {

    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option.top_num = '1';
    return client.logoSearch(image, option);


  }


  setanimalDetect(image, option = {}) {

    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.animalDetect(image, option);

  }

  setplantDetect(image, option = {}) {


    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.plantDetect(image, option);


  }


  setlandmark(image, option = {}) {

    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.landmark(image, option);

  }


  setgeneralBasic(image, option = {}) {

    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.generalBasic(image, option);

  }
  setwebimage(image, option = {}) {

    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.webImage(image, option);
  }


  setidcard(image, option = {}) {
    const idCardSide = option.idCardSide || 'front';
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.idcard(image, idCardSide);
  }


  sethandwriting(image, option = {}) {
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.handwriting(image, option);
  }

  setnumbers(image, option = {}) {
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    return client.numbers(image, option);
  }

}

window.onload = async () => {
  window.electron = new Initclass();
  const x = process.argv[process.argv.length - 1];
  try {
    if (path.extname(x) === '.sb3') {
      fs.accessSync(x, fs.constants.R_OK);
      fs.readFile(x, (err, file) => {
        vm.loadProject(file);
      });

      process.argv[process.argv.length - 1] = '';
    }
  } catch (err) {
    console.log(err);
  }
  document.addEventListener('drop', e => {
    e.preventDefault();
    e.stopPropagation();

    let fileToUpload = e.dataTransfer.files[0];
    const uploadedProjectTitle = getProjectTitleFromFilename(fileToUpload.name);
    if (!uploadedProjectTitle) return;
    readerx(fileToUpload);
  });
  document.addEventListener('dragover', function (e) {
    e.preventDefault();
    e.stopPropagation();
  });
};
